﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Spider.Rules;
using Spider.Entitys;

namespace Spider.Analyse
{
    public class 市规则解析 : AnalyseBase, IAnalyse
    {
        public override List<T> GetListData<T>(HtmlDocument doc, IRule rule, object o = null)
        {
            List<T> listEntity = new List<T>();
            //更加xpath获取总的对象，如果不为空，就继续选择dl标签
            HtmlNode res = doc.DocumentNode.SelectSingleNode(rule.RuleXPathString());
            if (res != null)
            {
                var list = res.SelectNodes(@"tr");//选择标签数组
                foreach (var item in list)
                {
                    var tdlist = item.SelectNodes(@"td");
                    var node = tdlist[1];
                    if (node != null &&
                        node.FirstChild != null &&
                        node.FirstChild.Name == "a")
                    {
                        CityEntity entity = new CityEntity()
                        {
                            GB11643_1999 = tdlist[0].InnerText,
                            CityName = node.InnerText,
                            CountyList = new List<CountyEntity>(),
                            Level = 2,
                            Url = node.InnerHtml.GetHref()
                        };
                        listEntity.Add((T)(object)entity);
                    }
                }
            }
            return listEntity;
        }
    }
}
